#include <iostream>
#include <memory>
#include "TcpServer.hpp"
#include "Socket.hpp"
#include "Accepter.hpp"

using namespace NetWorkModule;
const static int gbacklog = 32;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }

    int port = std::stoi(argv[1]);
    // 1.创建listensock
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->BuildListensocketMethod(port, gbacklog);

    // 2.创建tcpserver
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>();

    // 3.创建对应的获取连接的accepter
    std::unique_ptr<Accepter> accpter = std::make_unique<Accepter>();
    auto listen_rever = std::bind(&Accepter::AccepterConnection, accpter.get(), std::placeholders::_1);

    // 4.构建listen对应的connection，添加到tcpserver中去
    Connection *conn = ConnectionFactory::BuildListenConnection(listensock->GetSockfd(),
                                                                listen_rever,
                                                                EPOLLIN | EPOLLET, svr.get());
    svr->AddConnnection(conn);

    // 5.开始事件派发了
    svr->Dispatcher(); // 事件派发

    return 0;
}